package com.epower.web.action.app;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.epower.base.BaseAction;
import com.epower.bo.epower.commons.Constants;
import com.epower.bo.epower.commons.Md5Utils;
import com.epower.bo.epower.commons.Result;
import com.epower.bo.epoweruser.EpowerUserService;
import com.epower.dataobject.EpowerUserDO;

@Scope("prototype")
@Component("user")
public class UserAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private EpowerUserService epowerUserBO;
	private Result result = Result.succResult();
	private String username;
	private String password;
	private String mail;
	private Integer id;
	private Integer type;

	public String doLogin() throws JSONException {
		EpowerUserDO user = initUser();

		String error = checkUserNameAndPassWord(user);
		if (StringUtils.isNotBlank(error)) {
			result = Result.errorResult(Result.RESULTCODE_PARAMETER_ILLEGAL, error);
			return ERROR;

		}
		user = epowerUserBO.getEpowerUserByOption(user);

		if (user == null) {
			result = Result.errorResult(Result.RESULTCODE_PARAMETER_ILLEGAL, "username or password is error");
			return ERROR;
		}
		result.setResult(initResultUserMap(user));

		return SUCCESS;
	}

	public String doRegister() {
		EpowerUserDO user = initUser();
		StringBuilder error = new StringBuilder();
		error.append(checkUserNameAndPassWord(user)).append(checkMessage(user));
		if (error.length() > 0) {
			result = Result.errorResult(Result.RESULTCODE_PARAMETER_ILLEGAL, error.toString());
			return ERROR;
		}
		Integer userId = epowerUserBO.addEpowerUser(user);
		if (userId == null) {

			result = Result.errorResult(Result.RESULTCODE_REQUEST_FAILURE, "add user error");
			return ERROR;
		}
		Map<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("userid", String.valueOf(userId));
		result.setResult(resultMap);
		return SUCCESS;
	}

	public String doGetUser() {
		if (id == null && username == null) {
			result = Result.errorResult(Result.RESULTCODE_PARAMETER_ILLEGAL, "userid and username can't all be null");

			return ERROR;
		}
		EpowerUserDO user = epowerUserBO.getEpowerUserByOption(initUser());
		if (user == null) {
			result = Result.errorResult(Result.RESULTCODE_REQUEST_FAILURE, "user not exist");
			return ERROR;
		}

		result.setResult(initResultUserMap(user));

		return SUCCESS;
	}

	public String doUpdateUser() {

		if (id == null) {
			result = Result.errorResult(Result.RESULTCODE_PARAMETER_ILLEGAL, "userid and username can't all be null");

			return ERROR;
		}
		// 先根据userid查询出当前用户的信息
		EpowerUserDO user = epowerUserBO.getEpowerUserById(id);

		EpowerUserDO getUser = initUser();

		user.setMail(getUser.getMail());
		user.setPassword(getUser.getPassword());
		user.setUsername(getUser.getUsername());
		Map<String, Boolean> resultMap = new HashMap<String, Boolean>();
		if (!epowerUserBO.updateEpowerUserByOption(user)) {
			result = Result.errorResult(Result.RESULTCODE_REQUEST_FAILURE, "edit user error");
		}
		result.setResult(resultMap);
		return SUCCESS;

	}

	private EpowerUserDO initUser() {
		EpowerUserDO user = new EpowerUserDO();
		user.setId(id);
		user.setMail(mail);
		user.setUsername(username);
		if (password != null) {
			user.setPassword(Md5Utils.md5(password));
		}
		user.setStatus(Constants.STATUS_OK);
		user.setType(type == null ? Constants.USER_COMMON : type);
		return user;
	}

	private Map<String, String> initResultUserMap(EpowerUserDO user) {

		Map<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("userid", String.valueOf(user.getId()));
		resultMap.put("username", user.getUsername());
		resultMap.put("mail", user.getMail());
		resultMap.put("createtime", String.valueOf(user.getCreatetime().getTime()));
		resultMap.put("updatetime", String.valueOf(user.getUpdatetime().getTime()));
		resultMap.put("currentbook", String.valueOf(user.getCurrentbook()));
		return resultMap;

	}

	private String checkUserNameAndPassWord(EpowerUserDO user) {

		StringBuilder error = new StringBuilder();
		if (StringUtils.isEmpty(user.getUsername())) {
			error.append("username can't be null,");
		} else if (user.getUsername().trim().length() > 24) {
			error.append("username length can't bigger than 24,");
		}
		if (StringUtils.isEmpty(user.getPassword())) {
			error.append("password can't be null,");
		}
		if (error.length() > 0) {
			return error.substring(0, error.length() - 1);
		}
		return "";
	}

	private String checkMessage(EpowerUserDO user) {

		if (StringUtils.isBlank(user.getMail())) {

			return ",mail can't be null";
		} else {

			// 检测mail是否是正常的邮件格式

		}

		return "";

	}

	public Result getResult() {
		return result;
	}

	public void setResult(Result result) {
		this.result = result;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public void setEpowerUserBO(EpowerUserService epowerUserBO) {
		this.epowerUserBO = epowerUserBO;
	}

}
